package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.MaintainService;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Maintain;
import com.zhongkai.model3.Section;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 员工管理
 * */
@Service
@Transactional(rollbackFor = Exception.class)
public class MaintainServiceImpl implements MaintainService {


    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;

    /**
     * 添加员工
     *
     * @param tain_uuid    员工唯一标识
     * @param tain_name    姓名
     * @param tain_sex     性别
     * @param tain_section 部门
     * @param tain_phone   电话
     * @param tain_mail    邮箱
     */
    @Override
    public ZhongkaiResult addMaintain(String tain_uuid, String tain_name, String tain_sex, String tain_section, String tain_phone, String tain_mail) {
        if (StringUtils.isNullOrEmpty(tain_name) || StringUtils.isNullOrEmpty(tain_sex)
                || StringUtils.isNullOrEmpty(tain_section) || StringUtils.isNullOrEmpty(tain_phone)
                || StringUtils.isNullOrEmpty(tain_mail)

                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Section section;
        section = (Section) iBaseDao.get("from Section se where se.id='" + tain_section + "'");

        if (section == null) {
            return ZhongkaiResult.build(400, "该部门不存在");
        }else if (section.getSecName().contains("维修")){
            return ZhongkaiResult.build(400, "维修部员请在维修用户处操作");
        }

        Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.tainPhone='" + tain_phone + "' or ma.tainMail='" + tain_mail + "'");

        if (maintain != null) {
            return ZhongkaiResult.build(400, "手机号码或者邮箱不能重复");
        }



        maintain = new Maintain();
        maintain.setTainUuid(tain_uuid);
        maintain.setTainName(tain_name);
        maintain.setTainSex(tain_sex);
        maintain.setTainPhone(tain_phone);
        maintain.setTainStatus("1"); // 1 在职  2 离职 3请假
        maintain.setTainMail(tain_mail);




        long millis = new DateTime().getMillis(); //获取当前系统时间戳
        maintain.setTainSection(tain_section);
        maintain.setTainTime(millis);
        maintain.setTainRemove("1");
        maintain.setTainSectionName(section.getSecName());


        try {

            iBaseDao.save(maintain);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "添加失败");
        }
    }


    /**
     * 修改员工信息
     *
     * @param id           员工id
     * @param tain_name    姓名
     * @param tain_sex     性别
     * @param tain_section 部门
     * @param tain_phone   电话
     * @param tain_mail    邮箱
     */
    @Override
    public ZhongkaiResult updateMaintain(Long id, String tain_name, String tain_sex, String tain_section, String tain_phone, String tain_mail) {
        if (StringUtils.isNullOrEmpty(tain_name) || StringUtils.isNullOrEmpty(tain_sex)
                || StringUtils.isNullOrEmpty(tain_section) || StringUtils.isNullOrEmpty(tain_phone)
                || StringUtils.isNullOrEmpty(tain_mail) || id == 0

                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.id='" + id + "'");

        if (maintain == null) {
            return ZhongkaiResult.build(400, "此员工不存在");
        }

        Section section;
        section = (Section) iBaseDao.get("from Section se where se.id='" + tain_section + "'");

        if (section == null) {
            return ZhongkaiResult.build(400, "该部门不存在");
        }else if (section.getSecName().contains("维修")){
            return ZhongkaiResult.build(400, "维修部员请在维修用户处操作");
        }


        maintain.setTainName(tain_name);
        maintain.setTainSex(tain_sex);
        maintain.setTainPhone(tain_phone);
        maintain.setTainMail(tain_mail);

        maintain.setTainSection(tain_section);

        try {
            iBaseDao.update(maintain);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "修改失败");
        }

    }

    @Override
    public ZhongkaiResult removeMaintain(Long id) {  //删除员工

        if (id == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.id='" + id + "'");

        if (maintain == null) {
            return ZhongkaiResult.build(400, "此员工不存在");
        }


        Section section;
        section = (Section) iBaseDao.get("from Section se where se.id='" + maintain.getTainSection() + "'");

        if (section != null) {
            if (section.getSecName().contains("维修")){
                return ZhongkaiResult.build(400, "维修部员请在维修用户处操作");
            }
        }


    //    maintain.setTainPhone(maintain.getTainPhone()+"&");
      //  maintain.setTainRemove("2");

        try {
            iBaseDao.delete(maintain);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "删除失败");
        }
    }

    @Override
    public ZhongkaiResult updateMaintain2(Long id, String tain_status) { //修改员工状态

        if (id == 0 || StringUtils.isNullOrEmpty(tain_status)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        Maintain maintain = (Maintain) iBaseDao.get("from Maintain ma where ma.id='" + id + "'");

        if (maintain == null) {
            return ZhongkaiResult.build(400, "此员工不存在");
        }


        Section section;
        section = (Section) iBaseDao.get("from Section se where se.id='" + maintain.getTainSection() + "'");

        if (section != null) {
            if (section.getSecName().contains("维修")){
                return ZhongkaiResult.build(400, "维修部员请在维修用户处操作");
            }
        }

        maintain.setTainStatus(tain_status);

        try {
            iBaseDao.update(maintain);
            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(400, "修改失败");
        }
    }

    @Override
    public ZhongkaiResult listMaintain(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Maintain.class);
            detachedCriteria.add(Restrictions.eq("tainRemove","1")); //增加条件
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }

    }
}
